//
//  HistoryDabiaoViewController.m
//  GetPowerDBO
//
//  Created by 周博 on 2017/7/10.
//  Copyright © 2017年 BogoZhou. All rights reserved.
//

#import "HistoryDabiaoViewController.h"
#import "GTLCalendarView.h"


@interface HistoryDabiaoViewController ()<GTLCalendarViewDataSource, GTLCalendarViewDelegate>
{

}

@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,strong) GTLCalendarView *gtlCalendarView;
@end

@implementation HistoryDabiaoViewController

#pragma mark - GTLCalendarViewDataSource

//
- (NSDate *)minimumDateForGTLCalendar {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyy-MM-dd";
    NSString *minimumD = @"2017-06-01";
//    NSLog(@"minimumD -> %@,%@",_beginDate,minimumD);
    return [dateFormatter dateFromString:minimumD];
}

- (NSDate *)maximumDateForGTLCalendar {
    return [NSDate new];
}

//没错
- (NSDate *)defaultSelectFromDate {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyy-MM-dd";

//    NSString *beginD = @"2016-06-01";
//    NSLog(@"beginD -> %@,%@",_beginDate,_beginDate);
    return [dateFormatter dateFromString:_beginDate];
}

//没错
- (NSDate *)defaultSelectToDate {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyy-MM-dd";

//    NSString *endD = [BGDateHelper seeDayAroundToday:-1][0];
//    NSString *endD= @"2017-07-12";
//    NSLog(@"endD -> %@,%@",[BGDateHelper seeDayAroundToday:-1][0],endD);
    return [dateFormatter dateFromString:[BGDateHelper seeDayAroundToday:-1][0]];
}

#pragma mark - GTLCalendarViewDelegate

- (NSInteger)rangeDaysForGTLCalendar {
    return 30 * 6;
}

- (void)selectNSStringFromDate:(NSString *)fromDate toDate:(NSString *)toDate {
    NSLog(@"fromDate: %@, toDate: %@", fromDate, toDate);
}

#pragma mark - private instance method

#pragma mark * init values

- (void)setupGTLCalendarViews {
    CGRect frame = CGRectMake(0, 0, _viewContent.sizeWidth, _viewContent.sizeHeight);
    
    _gtlCalendarView = [[GTLCalendarView alloc] initWithFrame:frame];
    _gtlCalendarView.dataSource = self;
    _gtlCalendarView.delegate = self;
    _gtlCalendarView.myTypeValue = @"1";
    [_viewContent addSubview:_gtlCalendarView];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _dataArray = [NSMutableArray array];
    
    _imageViewGreen.layer.masksToBounds = YES;
    _imageViewGreen.layer.cornerRadius = _imageViewGreen.sizeWidth/2;
    
    _imageViewRed.layer.masksToBounds = YES;
    _imageViewRed.layer.cornerRadius = _imageViewRed.sizeWidth/2;
    
    [self setupGTLCalendarViews];
    [self loadData];
}

- (void)loadData{
    
    [self.hub hideAnimated:YES];
    
    self.hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    // Set the label text.
    self.hub.label.text = NSLocalizedString(@"加载中...", @"HUD loading title");
    NSLog(@" beginDate -> %@ , endDate - > %@",_beginDate,[BGDateHelper seeDayAroundToday:-1][0]);
    [[AFClient shareInstance] SingleShopSearchByEid:kUserId sid:_shopId start_date:_beginDate end_date:[BGDateHelper seeDayAroundToday:-1][0] progressBlock:nil success:^(id responseBody) {
        [self.hub hideAnimated:YES];
        NSLog(@"ddddddddddddddddddddd   ->    %@",responseBody);
        if ([responseBody[@"code"] integerValue] == 200) {
            [_dataArray removeAllObjects];
            for (NSDictionary *dic  in responseBody[@"data"]) {
                CurveModel *model = [[CurveModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];

                [_dataArray addObject:model];
            }
            NSLog(@"arrayCount => %ld",_dataArray.count);
            _gtlCalendarView.dataArray = _dataArray;
            [_gtlCalendarView reloadData];
        }else{
            kAlert(responseBody[@"message"]);
        }
        
    } failure:^(NSError *error) {
        [self.hub hideAnimated: YES];
    }];
}



- (IBAction)backButtonClick:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
